package com.g9.blog.controller;

import com.g9.blog.common.Response;
import com.g9.blog.common.Result;
import com.g9.blog.entity.Admin;
import com.g9.blog.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: Graycat
 * @Date: 2020/9/15 14:51
 * Create by lwj.
 */

@RestController
public class AdminController {

    @Autowired
    AdminService adminService;

    // 增改
    @PostMapping("/admin")
    public Result createOrUpdateAdmin(Admin admin){
        adminService.save(admin);
        return Response.makeOKRsp();
    }
    // 删除
    @DeleteMapping("/admin/{adminid}")
    public Result deleteAdminByid(@PathVariable int adminid){
        adminService.deleteById(adminid);
        return Response.makeOKRsp();
    }

    // 根据username查找管理员信息 --
    @GetMapping("/admin/byusername")
    public Result findAdminByUsername(String username){
        return Response.makeOKRsp(adminService.findByUsername(username));
    }

    // 根据id查找管理员信息
    @GetMapping("/admin/{id}")
    public Result findAdminByid(@PathVariable int id){
       return Response.makeOKRsp(adminService.findById(id));
    }
    // 返回所有管理员信息
    @GetMapping("/admin")
    public Result getAllAdmin(){
        return Response.makeOKRsp(adminService.findAll());
    }

    // 根据id上传管理员头像并修改数据库的管理员表-iron字段
    @PostMapping("/admin/upload")
    @ResponseBody
    public Result upload(MultipartFile file , int adminId  ) {
        Admin admin = adminService.findById(adminId);
        if (admin==null){
            return Response.makeErrRsp("无此管理员。");
        }
        return adminService.updateAdminIcon(file,admin);
    }
}
